<?php

namespace App\Http\Controllers\Api;

use App\Models\Report;
use App\Models\School;
use App\Services\FileService;
use App\Services\OCRService;
use App\Services\WechatService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * Class CommonController
 * @package App\Http\Controllers\Api
 */
class CommonController extends Controller
{
    protected $fileService;
    public function __construct(Request $request, FileService $fileService)
    {
        $this->fileService = $fileService;
        parent::__construct($request);
    }

    /**
     * 上传图片
     */
    public function uploadImage()
    {
        $this->fileService->uploadToRemote();
    }

    /**
     * 图片文字识别
     */
    public function ocr()
    {
        $result = $this->fileService->uploadImageToLocal();
        if (is_error($result)) {
            return $this->error($result['errno'], $result['message']);
        }
        //扫描结果为数组里面的字符串
        $data = OCRService::scan(ATTACHMENT_ROOT . $result['path']);
        foreach ($data['words_result'] as $word) {
            $words[] = $word['words'];
        }

        //图片上传到阿里云
        file_remote_upload($result['path']);

        return $this->success([
            'words' => $words,
            'url' => wgTomedia($result['path'])
        ]);
    }

    /**
     * 学校列表
     */
    public function schools()
    {
        $schools = School::select(['id', 'name'])->where('status', 1)->get();
        return $this->success($schools);
    }

    /**
     * 用户反馈
     */
    public function report()
    {
        $words = request()->input('words');
            $thumbs = request()->input('thumbs');

        if(empty($words)){
            return $this->error(-1,'描述文字不能为空');
        }

        Report::create([
            'uid'=>$this->uid,
            'words' =>$words,
            'thumbs' => $thumbs,
        ]);
        return  $this->success('','反馈成功，我们会认真查看您的反馈意见和建议');
    }

    /*注册协议协议*/
    public function reg_xieyi(){
        return view('xieyi.reg');
    }
    /*隐私协议协议*/
    public function ys_xieyi(){
        return view('xieyi.ys');
    }

}